package com.tyrant.macaulottery;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tyrant.macaulottery.activity.ShowActivityActivity;
import com.tyrant.macaulottery.adapter.BaseRecyclerAdapter;
import com.tyrant.macaulottery.adapter.BaseRecyclerViewHolder;
import com.tyrant.macaulottery.bean.BannerBean;
import com.tyrant.macaulottery.network.ApiLoader;
import com.tyrant.macaulottery.utils.ResultObserver;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 */
public class PreferentialFragment extends Fragment {


    @Bind(R.id.swipe_target)
    RecyclerView swipe_target;
    ArrayList<BannerBean> mdata = new ArrayList<BannerBean>();
    BaseRecyclerAdapter adapter;

    public PreferentialFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_preferential, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        swipe_target.setLayoutManager(layoutManager);//设置显示方式
        adapter = new BaseRecyclerAdapter<BannerBean>(getActivity(), mdata, R.layout.preferential_item) {
            @Override
            public void init(BaseRecyclerViewHolder holder, BannerBean bannerBean, int position) {
                ImageView view = holder.getView(R.id.iv_activity);
                Glide.with(getActivity()).load(bannerBean.getUrl())
//                        .fitCenter()
                        .into(view);
            }
        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(getActivity(), "mdata.get(position):" + mdata.get(position).getUrl(), Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(getActivity(), ShowActivityActivity.class);
                intent.putExtra("data", mdata.get(position).getActivity());
                startActivity(intent);
            }
        });
        swipe_target.setAdapter(adapter);//设置适配器

        new ApiLoader().getAds().subscribe(new ResultObserver<List<BannerBean>>() {
            @Override
            public void onNext(List<BannerBean> bannerBeens) {
                for (BannerBean bannerBeen : bannerBeens) {
                    mdata.add(bannerBeen);
                    adapter.notifyDataSetChanged();
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
